<?php

class unused_ref extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'unused_ref':
	 * @var integer $datetime
	 * @var string $ref
	 * @var integer $id
	 * @var integer $agent_id
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'unused_ref';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('ref','length','max'=>20),
			array('datetime, ref, id, agent_id', 'required'),
			array('datetime, id, agent_id', 'numerical', 'integerOnly'=>true),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
    public function scopes()
    {
        return array(
            'agent'=>array('condition'=>'agentid=0'),
            'year'=>array('condition'=>'datetime>0'),
            'ref'=>array('condition'=>'ref like "nothing"')
        );
    }
    public function agent($id=0)
    {
        if(!is_numeric($id))
            $id=0;
        $criteria = new CDbCriteria;
        $criteria->condition='agentid=:id';
        $criteria->params=array(':id'=>$id);
        $this->getDbCriteria()->mergeWith($criteria);
        return $this;
    }
    public function ref($ref='')
    {
        $criteria = new CDbCriteria;
        $criteria->condition='ref like :ref';
        $criteria->params=array(':ref'=>'%'.trim($ref).'%');
        $this->getDbCriteria()->mergeWith($criteria);
        return $this;
    }
    public function year($yr=0)
    {
        if(!is_numeric($yr))
            $yr=0;
        $criteria = new CDbCriteria;
        $lapseYr = mktime(0,0,0,1,1,($yr+1));
        $curYr=mktime(0,0,0,1,1,$yr);
        $criteria->condition = 'datetime >= :curtime and datetime < :lapseyr';
        $criteria->params = array(':curtime'=>$curYr,'lapseyr'=>$lapseYr);
        $this->getDbCriteria()->mergeWith($criteria);
        return $this;
    }
	public function attributeLabels()
	{
		return array(
			'datetime'=>'Datetime',
			'ref'=>'Ref',
			'id'=>'Id',
			'agent_id'=>'Agent',
		);
	}
}